package com.heima.behavior.service.impl;

import com.heima.behavior.service.ApArticleBehaviorService;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.model.behavior.dtos.ArticleBehaviorDTO;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.behavior.pojos.ApUnlikesBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.pojos.ApUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
@Service
public class ApArticleBehaviorServiceImpl implements ApArticleBehaviorService {
    @Autowired
    ApBehaviorEntryService apBehaviorEntryService;
    @Autowired
    MongoTemplate mongoTemplate;

    @Override
    public ResponseResult loadArticleBehavior(ArticleBehaviorDTO dto) {
       //1. 判断用户是否登录
        ApUser user = AppThreadLocalUtils.getUser();
        Map<Object, Object> map = new HashMap<>();
        map.put("islike",true);
        map.put("isfollow",true);
        map.put("isunlike",true);
        map.put("iscollection",true);
        //2. 未登录 直接将 4种行为 均返回false
        if (user == null) {
            map.put("islike",false);
            map.put("isfollow",false);
            map.put("isunlike",false);
            map.put("iscollection",false);
            return ResponseResult.okResult(map);
//            throw new CustomException(AppHttpCodeEnum.NEED_LOGIN);
        }
        Integer userId = user.getId();
       //3. 如果登录

        //4. 查询行为实体
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(userId, dto.getEquipmentId());

       //5. 根据行为实体 文章ID查询 是否点赞
        Query query1 = Query.query(Criteria.where("entryId").is(apBehaviorEntry.getId()).and("articleId").is(dto.getArticleId()));
        ApLikesBehavior one = mongoTemplate.findOne(query1, ApLikesBehavior.class);
        if (one == null) {
            map.put("islike",false);
        }
        //6. 根据行为实体 文章ID查询 是否不喜欢
        Query query2 = Query.query(Criteria.where("entryId").is(apBehaviorEntry.getId()).and("articleId").is(dto.getArticleId()));
        ApUnlikesBehavior two = mongoTemplate.findOne(query2, ApUnlikesBehavior.class);
        if (two == null) {
            map.put("isunlike",false);
        }
        //7. 根据行为实体 文章ID查询 是否收藏
        Query query3 = Query.query(Criteria.where("entryId").is(apBehaviorEntry.getId()).and("articleId").is(dto.getArticleId()));
        ApLikesBehavior three = mongoTemplate.findOne(query3, ApLikesBehavior.class);
        if (three == null) {
            map.put("iscollection",false);
        }
        //8. 更具登录用户id 去redis中查询是否关注该作者
        map.put("isfollow",false);

       //9. 封装结果 返回


        return ResponseResult.okResult(map);
    }
}
